package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;

import com.itheima.log.LogAnno;
import com.itheima.pojo.HotSetmealVo;
import com.itheima.pojo.ReportDataVo;
import com.itheima.pojo.Result;
import com.itheima.service.ReportService;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/report")
public class ReportController {
    @Reference
    ReportService reportService;

    @LogAnno("查询套餐报表")
    @PreAuthorize("hasAnyAuthority('REPORT_VIEW')")
    @RequestMapping("/getSetmealReport")
    public Result getSetmealReport() {
        try {List<Map<String,String>> map = reportService.getSetmealReport();
            for (Map<String, String> stringStringMap : map) {
                if (stringStringMap.get("name")==null){
                    stringStringMap.put("name","未知性别");
                }else if("1".equals(stringStringMap.get("name"))){
                    stringStringMap.put("name","男");
                }else {
                    stringStringMap.put("name","女");
                }
            }
            return Result.success("查询套餐统计成功", map);
        } catch (Exception e) {
            return Result.error("查询套餐统计失败");
        }
    }
    @RequestMapping("/getAgeNummber")
    public Result getAgeNummber(){
        List<Map<String,String>> map = reportService.getAgeNummber();
        return Result.success("", map);
    }

    @LogAnno("查询运营报表")
    @PreAuthorize("hasAnyAuthority('REPORT_VIEW')")
    @RequestMapping("/getBusinessReportData")
    public Result getBusinessReportData() {
        try {
            ReportDataVo reportDataVo = reportService.getBusinessReportData();
            return Result.success("查询运营数据成功", reportDataVo);
        } catch (Exception e) {
            return Result.error("查询运营数据失败");
        }
    }

    @LogAnno("导出运营报表")
    @PreAuthorize("hasAnyAuthority('REPORT_VIEW')")
    @RequestMapping("/exportBusinessReport")
    public void exportBusinessReport(HttpServletRequest request, HttpServletResponse response) {
        try {
            ReportDataVo vo = reportService.getBusinessReportData();
            String realPath = request.getSession().getServletContext().getRealPath("template")+"/report_template.xlsx";
            XSSFWorkbook sheets = new XSSFWorkbook(new FileInputStream(new File(realPath)));
            XSSFSheet sheetAt = sheets.getSheetAt(0);
            sheetAt.getRow(2).getCell(5).setCellValue(vo.getReportDate());
            sheetAt.getRow(4).getCell(5).setCellValue(vo.getTodayNewMember());
            sheetAt.getRow(4).getCell(7).setCellValue(vo.getTotalMember());
            sheetAt.getRow(5).getCell(5).setCellValue(vo.getThisWeekNewMember());
            sheetAt.getRow(5).getCell(7).setCellValue(vo.getThisMonthNewMember());
            sheetAt.getRow(7).getCell(5).setCellValue(vo.getTodayOrderNumber());
            sheetAt.getRow(7).getCell(7).setCellValue(vo.getTodayVisitsNumber());
            sheetAt.getRow(8).getCell(5).setCellValue(vo.getThisWeekOrderNumber());
            sheetAt.getRow(8).getCell(7).setCellValue(vo.getThisWeekVisitsNumber());
            sheetAt.getRow(9).getCell(5).setCellValue(vo.getThisMonthOrderNumber());
            sheetAt.getRow(9).getCell(7).setCellValue(vo.getThisMonthVisitsNumber());

            List<HotSetmealVo> hotSetmeal = vo.getHotSetmeal();
            int i = 12;
            for (HotSetmealVo hotSetmealVo : hotSetmeal) {
                sheetAt.getRow(i).getCell(4).setCellValue(hotSetmealVo.getName());
                sheetAt.getRow(i).getCell(5).setCellValue(hotSetmealVo.getSetmealCount());
                sheetAt.getRow(i).getCell(6).setCellValue(hotSetmealVo.getProportion().doubleValue());
                i++;
            }

            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");//application/json
            response.setHeader("Content-Disposition","attachment;filename="+ java.net.URLEncoder.encode("运营数据报表", "UTF-8")+".xlsx");

            ServletOutputStream outputStream = response.getOutputStream();
            sheets.write(outputStream);

            outputStream.flush();
            outputStream.close();
            sheets.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
